package pl;


public abstract class AbstractDAO<E,K> {
	public ConexionBBDD conn;
	protected K key;
	protected Class<E> entity;

    public AbstractDAO(final Class<E> entity, K key) throws BaseDatosException{
    	this.entity = entity; 
    	this.key = key;
    	conn = new ConexionBBDD();
    }
    
    public void insert(E e) throws BaseDatosException {
        ConexionBBDD conn = new ConexionBBDD();

        try {
                conn.kem.getTransaction().begin();
                conn.kem.persist(e);
                conn.kem.getTransaction().commit();
        } finally {
                conn.cerrarConexion();
        }
    }
    
    public void delete() throws BaseDatosException {
        ConexionBBDD conn = new ConexionBBDD();

        try {
                conn.kem.getTransaction().begin();
                E aux = (E)conn.kem.find(this.entity, this.key);                     
                conn.kem.remove(aux);
                conn.kem.getTransaction().commit();
        } finally {
                conn.cerrarConexion();
        }
}
    
    public void modify(E e) throws BaseDatosException {
    	ConexionBBDD conn = new ConexionBBDD();

    	try {
    		conn.kem.getTransaction().begin();
    		conn.kem.merge(e);
    		conn.kem.getTransaction().commit();
    	} finally {
    		conn.cerrarConexion();
    	}
    }
    
	
	
}
